//SPDX-FileCopyrightText: Copyright 2025-2025 深圳市同心圆网络有限公司
//SPDX-License-Identifier: GPL-3.0-only

import { invoke } from "@tauri-apps/api/core";

export type UserPerm = {
    can_update_perm: boolean;
    can_remove: boolean;
};

export type MemberInfo = {
    member_user_id: string;
    board_id: string;
    last_view_time: number;
    member_display_name: string;
    member_logo_uri: string;
    admin: boolean;
    user_perm: UserPerm;

};

export type GenInviteRequest = {
    session_id: string;
    board_id: string;
    ttl_hour: number;
};

export type GenInviteResponse = {
    code: number;
    err_msg: string;
    invite_code: string;
}

export type JoinRequest = {
    session_id: string;
    invite_code: string;
};

export type JoinResponse = {
    code: number;
    err_msg: string;
    board_id: string;
}

export type SetAdminRequest = {
    session_id: string;
    board_id: string;
    member_user_id: string;
    admin: boolean;
};

export type SetAdminResponse = {
    code: number;
    err_msg: string;
};

export type UpdateViewTimeRequest = {
    session_id: string;
    board_id: string;
};

export type UpdateViewTimeResponse = {
    code: number;
    err_msg: string;
};

export type RemoveRequest = {
    session_id: string;
    board_id: string;
    member_user_id: string;
};

export type RemoveResponse = {
    code: number;
    err_msg: string;
};

export type LeaveRequest = {
    session_id: string;
    board_id: string;
};

export type LeaveResponse = {
    code: number;
    err_msg: string;
};

export type ListMyRequest = {
    session_id: string;
};

export type ListMyResponse = {
    code: number;
    err_msg: string;
    member_list: MemberInfo[];
};

export type ListRequest = {
    session_id: string;
    board_id: string;
};

export type ListResponse = {
    code: number;
    err_msg: string;
    member_list: MemberInfo[];
};

export type GetRequest = {
    session_id: string;
    board_id: string;
    member_user_id: string;
};

export type GetResponse = {
    code: number;
    err_msg: string;
    member: MemberInfo;
};

// 生成加入邀请码
export async function gen_invite(request: GenInviteRequest): Promise<GenInviteResponse> {
    const cmd = 'board_member_api_cmds_gen_invite';
    console.log(`%c${cmd}`, 'color:#0f0;', request);
    return invoke<GenInviteResponse>(cmd, {
        request,
    });
}

// 加入团队
export async function join(request: JoinRequest): Promise<JoinResponse> {
    const cmd = 'board_member_api_cmds_join';
    console.log(`%c${cmd}`, 'color:#0f0;', request);
    return invoke<JoinResponse>(cmd, {
        request,
    });
}

// 设置管理员标记
export async function set_admin(request: SetAdminRequest): Promise<SetAdminResponse> {
    const cmd = 'board_member_api_cmds_set_admin';
    console.log(`%c${cmd}`, 'color:#0f0;', request);
    return invoke<SetAdminResponse>(cmd, {
        request,
    });
}

// 更新最后访问时间
export async function update_view_time(request: UpdateViewTimeRequest): Promise<UpdateViewTimeResponse> {
    const cmd = 'board_member_api_cmds_update_view_time';
    console.log(`%c${cmd}`, 'color:#0f0;', request);
    return invoke<UpdateViewTimeResponse>(cmd, {
        request,
    });
}

// 移除成员
export async function remove(request: RemoveRequest): Promise<RemoveResponse> {
    const cmd = 'board_member_api_cmds_remove';
    console.log(`%c${cmd}`, 'color:#0f0;', request);
    return invoke<RemoveResponse>(cmd, {
        request,
    });
}

// 离开白板
export async function leave(request: LeaveRequest): Promise<LeaveResponse> {
    const cmd = 'board_member_api_cmds_leave';
    console.log(`%c${cmd}`, 'color:#0f0;', request);
    return invoke<LeaveResponse>(cmd, {
        request,
    });
}

// 列出所有关于我的成员信息
export async function list_my(request: ListMyRequest): Promise<ListMyResponse> {
    const cmd = 'board_member_api_cmds_list_my';
    console.log(`%c${cmd}`, 'color:#0f0;', request);
    return invoke<ListMyResponse>(cmd, {
        request,
    });
}

// 列出成员
export async function list(request: ListRequest): Promise<ListResponse> {
    const cmd = 'board_member_api_cmds_list';
    console.log(`%c${cmd}`, 'color:#0f0;', request);
    return invoke<ListResponse>(cmd, {
        request,
    });
}

// 获取单个成员
export async function get(request: GetRequest): Promise<GetResponse> {
    const cmd = 'board_member_api_cmds_get';
    console.log(`%c${cmd}`, 'color:#0f0;', request);
    return invoke<GetResponse>(cmd, {
        request,
    });
}